
var layer = layui.layer //弹层
    ,table = layui.table //表格
    ,form = layui.form,$ = layui.jquery,laydate = layui.laydate;



lay('.starttime').each(function(){
    laydate.render({
        elem: this
    });
});
layer.ready(function () {
    //获取所有用户信息
    $.ajax({
        url:"otherOutStock/getAllUserInfo",
        success:function(data){
            if(data.status == 200){
                for(let i in data.data){
                    $('#operationNo').append(new Option(data.data[i].name, data.data[i].code));
                }
                $('#operationNo').val(user.code);
                form.render('select');
            }
        }
    });

    //获取所有公司信息
    $.ajax({
        url:"receivePayment/getCompanyInfo",
        success:function(data){
            if(data.status == 200){
                for(let i in data.data){
                    //公司名称
                    $('#signingCompanyCode').append(new Option(data.data[i].name, data.data[i].code));
                }
                form.render('select');
            }
        }
    });
});
form.render();//表单刷新

let initData = [];
table.render({
    elem: '#offerselect'
    ,data:initData
    ,where:{operationNo : user.code}
    ,url:'offer/getListByPage'
    ,toolbar: '#offertoolbar'
    ,cols: [[
        //采购子表为主
        {checkbox: true, fixed: true}
        ,{field:'billNo', title:'采购单号',sort:true,width: 220,event: "contractno"}
        ,{field:'customerName', title:'供应商名称',sort:true,width: 120}
        ,{field:'customerName', title:'供应商编码',sort:true,width: 120}
        ,{field:'customerAccountingPeriod', title:'供应商账期',sort:true,width: 120}
        ,{field:'signingDate', title:'签约日期',sort:true,width: 120}
        ,{field:'salesmanName', title:'产品编码',sort:true,width: 120}
        ,{field:'salesmanName', title:'规格编码',sort:true,width: 120}
        ,{field:'salesmanName', title:'中文名称',sort:true,width: 120}
        ,{field:'salesmanName', title:'采购数量',sort:true,width: 120}
        ,{field:'salesmanName', title:'出运计划数量',sort:true,width: 120}
        ,{field:'salesmanName', title:'未计划数量',sort:true,width: 120}
        ,{field:'salesmanName', title:'出运计划单号',sort:true,width: 120}
        ,{field:'salesmanName', title:'出运明细数量',sort:true,width: 120}
        ,{field:'salesmanName', title:'未明细数量',sort:true,width: 120}
        ,{field:'signingCompanyName', title:'签约公司',sort:true,width: 220}
        ,{field:'salesmanName', title:'业务员',sort:true,width: 120}
        ,{field:'salesmanName', title:'操作员',sort:true,width: 120}
    ]]
    ,id: 'offertableid'
    ,height: $(document).height() - $('#offerselect').offset().top - 50
    ,page: false
});
table.on('tool(offertable)', function(obj){
    let data = obj.data;
    let id = data.id;
    if(obj.event === 'update') {
        parent.intab("views/jsp/ExportBusiness/tpl/updateOffer.jsp?id="+obj.data.id,"修改报价")
    }else if(obj.event === 'delect'){
        layer.confirm('确认删除？',confirmInfo(3,6), function (index) {
            layer.close(index);
            let dataBases={
                id:id,
                status:data.status
            }
            $.ajax({
                type:"POST",
                dataType:"json",
                contentType: 'application/json',
                data:JSON.stringify(dataBases),
                url:"offer/delete",
                success:function(res){
                    if(res.status == 200){
                        layer.msg("删除成功", {icon: 6});
                        table.reload('offertableid', {
                            page: {
                                limit:10,
                                curr: 1 //重新从第 1 页开始
                            }
                            ,where: data.field
                        }, 'data');
                        return false;
                    }else {
                        layer.msg(res.msg, {icon: 5});
                    }
                }
            });
        });
    }else if(obj.event === 'submits'){
        layer.confirm('确定提交审批吗', confirmInfo(3, 5), function (index) {
            startPurchaseProcess(id);
            operationData(data, 1, "提交审批");
        });
    }else if(obj.event === 'through'){
        layer.confirm('确定审批通过吗', confirmInfo(3, 5), function (index) {
            operationData(data, 2, "审批通过");
        });
    }else if(obj.event === 'backtothe'){
        layer.confirm('确定审批退回吗', confirmInfo(3, 5), function (index) {
            operationData(data, 3, "审批退回");
        });
    }else if(obj.event === 'complete'){
        layer.confirm('确定完成吗', confirmInfo(3, 5), function (index) {
            operationData(data, 4, "完成");
        });
    }else if(obj.event === 'reduction'){
        layer.confirm('确定还原吗', confirmInfo(3, 5), function (index) {
            operationData(data, 5, "还原");
        });
    }else if (obj.event === "quotationno"){
        parent.intab("views/jsp/ExportBusiness/tpl/viewoffer.jsp?id="+obj.data.id,"报价明细")
    }
});

//审批流开始，获取流程ID
function startPurchaseProcess(bid) {
    let res;
    $.ajax({
        type : 'get',
        data:{"processDefId":'offer',"bid":bid,"variables":null},
        url:'process/startProcess',
        dataType:"json",
        contentType: "application/x-www-form-urlencoded; charset=utf-8",
        async:false,
        success : function (da) {
            if(da.status==200 ){
                res=da.data;
            }
        },
        error: function(data) {
            // layer.msg("获取失败!");
        }
    });
    return res;
}

function operationData(data, operation, msg){
    let fields = {
        id : data.id,
        billNo : data.billNo,
        operation: operation
    }
    $.ajax({
        type:"POST",
        dataType:"json",
        contentType: 'application/json',
        data:JSON.stringify(fields),
        url:"offer/operation",
        success:function(res){
            if(res.status == 200){
                layer.msg(msg, {icon: 6});
                table.reload('offertableid', {
                    page: {
                        limit:10,
                        curr: 1 //重新从第 1 页开始
                    }
                    ,where: data.field
                }, 'data');
                return false;
            }else {
                layer.msg(res.msg, {icon: 5});
            }
        }
    });
}

form.on('submit(submitform)', function(data){
    table.reload('offertableid', {
        where: data.field
    }, 'data');
    return false;
});

table.on('toolbar(offertable)', function(obj){
    let checkStatus = table.checkStatus(obj.config.id);
    switch(obj.event){
        case 'news':
            parent.intab("views/jsp/ExportBusiness/tpl/newupoffer.jsp","新增报价")
            break;
        case 'dayin':
            if(checkStatus.data.length > 1){
                layer.msg( "每次只能打印一个报价单据! ");
                break;
            }
            if(checkStatus.data.length == 0){
                layer.msg("请选择要打印的报价单据! ");
                break;
            }
            window.open("views/jsp/report.jsp?ym=offerReport&code=id:"+checkStatus.data[0].id)
            break;
    };
});

$("#statistical").on("click",function () {
    parent.intab("views/jsp/ExportBusiness/tpl/offerStatistics.jsp","报价统计")
});

//提示信息渲染
function confirmInfo(icon, anim) {
    let data = {
        icon: icon,
        title: "提示",
        offset: "auto",
        skin: 'layui-layer-molv',
        anim: anim
    }
    return data;
}